# 测试用户行为监控功能是否已被禁用
Write-Host "=== 测试用户行为监控功能禁用状态 ===" -ForegroundColor Green

# 1. 检查后端依赖注入是否已注释
Write-Host "`n1. 检查后端依赖注入配置..." -ForegroundColor Yellow
$programCs = Get-Content "Program.cs"
$behaviorInjections = $programCs | Select-String "IUserBehavior" | Where-Object { $_ -notmatch "^\s*//" }
if ($behaviorInjections.Count -eq 0) {
    Write-Host "✓ 后端依赖注入已正确注释" -ForegroundColor Green
} else {
    Write-Host "✗ 发现未注释的依赖注入:" -ForegroundColor Red
    $behaviorInjections | ForEach-Object { Write-Host "  $_" -ForegroundColor Red }
}

# 2. 检查前端服务是否已禁用
Write-Host "`n2. 检查前端用户行为服务..." -ForegroundColor Yellow
$frontendService = Get-Content "RefundAuditWeb\src\services\userBehaviorService.js"
$activeServices = $frontendService | Select-String "track|start|complete" | Where-Object { $_ -notmatch "^\s*//" -and $_ -notmatch "DisabledUserBehaviorService" }
if ($activeServices.Count -eq 0) {
    Write-Host "✓ 前端用户行为服务已正确禁用" -ForegroundColor Green
} else {
    Write-Host "✗ 发现未禁用的前端服务方法:" -ForegroundColor Red
    $activeServices | ForEach-Object { Write-Host "  $_" -ForegroundColor Red }
}

# 3. 检查Vue组件中的调用是否已注释
Write-Host "`n3. 检查Vue组件中的用户行为调用..." -ForegroundColor Yellow
$vueFile = Get-Content "RefundAuditWeb\src\views\RefundTaskManagement.vue"
$activeCalls = $vueFile | Select-String "userBehaviorService\." | Where-Object { $_ -notmatch "^\s*//" }
if ($activeCalls.Count -eq 0) {
    Write-Host "✓ Vue组件中的用户行为调用已正确注释" -ForegroundColor Green
} else {
    Write-Host "✗ 发现未注释的用户行为调用:" -ForegroundColor Red
    $activeCalls | ForEach-Object { Write-Host "  $_" -ForegroundColor Red }
}

# 4. 检查最新日志中是否还有用户行为错误
Write-Host "`n4. 检查最新日志..." -ForegroundColor Yellow
$logFile = "logs\service-$(Get-Date -Format 'yyyyMMdd').log"
if (Test-Path $logFile) {
    $recentErrors = Get-Content $logFile | Select-String "UserBehavior|会话.*不存在|会话.*暂未找到" | 
                   Where-Object { $_.Line -match "$(Get-Date -Format 'HH:mm')" } | Select-Object -Last 5
    if ($recentErrors.Count -eq 0) {
        Write-Host "✓ 最新日志中没有用户行为监控错误" -ForegroundColor Green
    } else {
        Write-Host "✗ 发现最新的用户行为监控错误:" -ForegroundColor Red
        $recentErrors | ForEach-Object { Write-Host "  $_" -ForegroundColor Red }
    }
} else {
    Write-Host "! 未找到今天的日志文件" -ForegroundColor Yellow
}

# 5. 测试API端点是否返回正确响应
Write-Host "`n5. 测试用户行为API端点..." -ForegroundColor Yellow
try {
    $response = Invoke-WebRequest -Uri "http://localhost:5000/api/UserBehavior/track" -Method POST -ContentType "application/json" -Body '{"sessionId":"test","actionType":"TEST"}' -ErrorAction SilentlyContinue
    if ($response.StatusCode -eq 200) {
        Write-Host "✓ API端点响应正常（返回200）" -ForegroundColor Green
    } else {
        Write-Host "! API端点返回状态码: $($response.StatusCode)" -ForegroundColor Yellow
    }
} catch {
    Write-Host "! 无法连接到API端点，可能服务未启动或端口不同" -ForegroundColor Yellow
}

Write-Host "`n=== 测试完成 ===" -ForegroundColor Green
Write-Host "如果所有项目都显示 ✓，则用户行为监控功能已成功禁用" -ForegroundColor Cyan